'use strict';
// let data = '[CQ:at,qq=1544392599] 搜赛马娘'
let reg = /^\[CQ:at,qq=1544392599] 搜(.*?)$/;
// let reg = /\[CQ:reply,id=(.*?)]\[CQ:at,qq=1544392599] \[CQ:at,qq=1544392599] 撤回$/;

const Service = require('egg').Service;

class SearchImgService extends Service {
  async regular(res) {
    let str = res.message;
    return reg.test(str);
  }

  async searchImg(res) {
    let sendMsg = this.ctx.service.sendMsg;
    let key = res.message.match(reg);
    key = key[1];
    try {
      let result = await this.ctx.curl(`https://api.lolicon.app/setu/?r18=2&apikey=885915936065db81372f56&size1200=true&keyword=${key}`, {
        contentType: 'json'
      });
      result = JSON.parse(result.data.toString());
      sendMsg.sendToGroup({
        id: res.group_id,
        msg: `[CQ:image,file=${result.data[0].url}]`
      })
    } catch (error) {
      sendMsg.sendToGroup({
        id: res.group_id,
        msg: `没有搜到哦`,
      })
    }

  }
}

module.exports = SearchImgService;